package com.mes.web.controller.biz;

import cn.hutool.core.util.IdUtil;
import com.github.pagehelper.PageInfo;
import com.mes.biz.domain.BizPayable;
import com.mes.biz.domain.BizPurchase;
import com.mes.biz.domain.BizPurchaseDetail;
import com.mes.biz.domain.BizSupplierPayable;
import com.mes.biz.mapper.BizPurchaseMapper;
import com.mes.biz.service.BizPayableService;
import com.mes.biz.service.BizPurchaseService;
import com.mes.biz.service.BizSupplierPayableService;
import com.mes.common.annotation.Log;
import com.mes.common.core.controller.BaseController;
import com.mes.common.core.domain.BaseEntity;
import com.mes.common.core.domain.R;
import com.mes.common.core.page.PageDomain;
import com.mes.common.enums.BusinessType;
import com.mes.framework.cache.DictUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 付账单Controller
 * @author wy
 * @email wy
 * @date 2023-05-19
 */
@RestController
@RequestMapping("/biz/bizPayable")
public class BizPayableController extends BaseController {

    @Autowired
    private BizPayableService bizPayableService;
    /**
     * 应付账款列表
     */
    @GetMapping("/list")
    public R<PageInfo> list(BizPayable bizPayable, HttpServletRequest request, HttpServletResponse response) {
        bizPayable.setPage(new PageDomain(request, response));
        return R.data(bizPayableService.findPage(bizPayable));
    }

    /**
     * 新增付款单
     */
    @Log(title = "新增付款单", businessType = BusinessType.INSERT)
    @PostMapping
    public R add(@RequestBody @Validated BizPayable bizPayable) {
        return R.status(bizPayableService.createPayable(bizPayable));
    }

    /**
     * 修改付款单
     */
    @Log(title = "修改付款单", businessType = BusinessType.UPDATE)
    @PutMapping
    public R edit(@RequestBody @Validated BizPayable bizPayable) {
        return R.status(bizPayableService.updatePayable(bizPayable));
    }

    /**
     * 删除付款单
     */
    @Log(title = "删除付款单", businessType = BusinessType.DELETE)
    @Transactional(rollbackFor = Exception.class)
    @DeleteMapping("/{id}")
    public R remove(@PathVariable String id) {
        return R.status(bizPayableService.deletePayable(id));
    }

    /**
     * 根据字典类型查询字典数据信息等其他自定义信息
     */
    @GetMapping(value = "/getInitData/{dictTypes}")
    public R getInitData(@PathVariable String dictTypes) {
        Map<String, Object> dataMap = new HashMap<String, Object>();
        dataMap.putAll(DictUtils.getMultiDictList(dictTypes));
        return R.data(dataMap);
    }
}
